<?php
class ClasseDAO{
	public $db;

	public function __construct ($db){
		$this->db = $db;
	}

	public function setDb($db){
		$this->db = $db;
	}

	public function getDb(){
		return $this->db;
	}

	public function addClasse($c){
		$sql = "insert into classe (descr)
				values ('{$c->getDescr()}')";		
		$res = $this->getDb()->insert($sql);
		return $res;		
	}

	public function setClasse($c){
		$sql = "update classe set
				descr = '{$c->getDescr()}'
				where classe = {$c->getId()}";
		$res = $this->getDb()->update($sql);
		return $res;
	}

	public function getClasse($id){
		$sql = "select CLASSE, DESCR
				from classe
				where classe = {$id}";
		$res = $this->getDb()->query($sql);
		$c = new Classe($res[0]['CLASSE'],$res[0]['DESCR']);

		return $c;		
	}

	public function getLista($where=array()){
		$lista = array();
		$where = ($where ? "where ".(implode(" and ",$where)) : "");

		$sql = "select CLASSE, DESCR
					from classe 
					{$where}
					order by DESCR";
		$res = $this->getDb()->query($sql);

		if($res){
			foreach($res as $r){
				$lista[] = new Classe($r['CLASSE'],$r['DESCR']);
			}
		}

		return $lista;
	}
}
?>